package com.lijianlove.message.dal;

import com.lijianlove.message.common.model.RabbitMetaMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author lijian
 * @date 2019/5/29 下午8:33
 */
@Slf4j
@Service
public class RabbitMetaMessageServiceImpl implements RabbitMetaMessageService {


    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public void save(RabbitMetaMessage msg) {
        if (ObjectUtils.isEmpty(msg)) {
            throw new RuntimeException("msg 不能为空");
        }
        // todo lj 验证
    }

    @Override
    public RabbitMetaMessage get(String uniqueKey) {

        RabbitMetaMessage rabbitMetaMessage = jdbcTemplate.queryForObject("select exchange,routingKey,queue,bindingKey,verifyUrl,uniqueKey from message_meta where uniqueKey='" + uniqueKey + "'", (resultSet, i) -> {
            RabbitMetaMessage metaMessage = new RabbitMetaMessage();
            metaMessage.setBindingKey(resultSet.getString("bindingKey"));
            metaMessage.setExchange(resultSet.getString("exchange"));
            metaMessage.setQueue(resultSet.getString("queue"));
            metaMessage.setRoutingKey(resultSet.getString("routingKey"));
            metaMessage.setVerifyUrl(resultSet.getString("verifyUrl"));
            metaMessage.setUniquKey(resultSet.getString("uniqueKey"));
            return metaMessage;
        });

        return rabbitMetaMessage;
    }
}
